/**   
 *    
 * 项目名称：HelpService   
 * 类名称：CommFun   
 * 类描述：   
 * 创建人：Echo   
 * 创建时间：2014年6月12日 下午3:00:45   
 * 修改人：Echo   
 * 修改时间：2014年6月12日 下午3:00:45   
 * 修改备注：   
 * @version    
 *    
 */
package com.lst.common;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.util.Properties;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

import sun.misc.BASE64Decoder;

import com.lst.model.vo.MailVo;
import com.lst.servlet.IMailServlet;
import com.lst.utils.CommonUtils;
import com.lst.utils.DateUtil;
import com.lst.zx.webservice.Sms_Service;

/**
 * @ClassName: CommFun
 * @Description: Common tools
 * @author Echo
 * @date 2014年6月12日 下午3:00:45
 * 
 */
@SuppressWarnings("restriction")
public class CommFun {

	private static BASE64Decoder decoder = new BASE64Decoder();

	protected static Logger log = Logger.getLogger("log");

	/**
	 * 
	 * @Title: Filter
	 * @Description: Convert the Chinese
	 * @param @param paramter
	 * @param @return 设定文件
	 * @return String 返回类型
	 * @throws
	 */
//	public static String Filter(String paramter) {
//		String toUtf = "";
//		try {
//			toUtf = new String(paramter.getBytes("ISO-8859-1"), "UTF-8");
//		} catch (UnsupportedEncodingException e) {
//
//			e.printStackTrace();
//		}
//
//		return toUtf;
//
//		// return paramter;
//	}

	public static String[] FilterArray(String[] strs) {
		// try {
		// for (int i = 0; i < strs.length; i++) {
		// strs[i] = new String(strs[i].getBytes("iso-8859-1"),"utf-8");
		// }
		// } catch (Exception e) {
		// e.printStackTrace();
		// }

		return strs;
	}

	/**
	 * 
	 * @Title: makeRandomWithRange
	 * @Description: Make the random code
	 * @param @param min
	 * @param @param max
	 * @param @return 设定文件
	 * @return int 返回类型
	 * @throws
	 */
	public static int makeRandomWithRange(int min, int max) {

		Random random = new Random();

		int num = random.nextInt(max) % (max - min + 1) + min;

		return num;
	}

	/**
	 * 
	 * @Title: send
	 * @Description: send
	 * @param @param id
	 * @param @param message
	 * @param @param mobileNo
	 * @param @return 设定文件
	 * @return boolean 返回类型
	 * @throws
	 */
	public static boolean send(int id, String message, String mobileNo) {
		boolean bclExcuteOk = false;

		Sms_Service smsService = new Sms_Service();

		if (StringUtils.isNotEmpty(message)) {

			String result = smsService.getSmsHttpPort().sms(
					CommonUtils.getEnterNo(), CommonUtils.getUsername(),
					CommonUtils.getUserpassword(), message, mobileNo, "", "",
					"", "", "", "");

			// result=0&description=发送短信成功&taskid=10131276929&faillist=
			String[] resultArry = result.split("&");
			String zero = resultArry[0].replaceAll("result=", "");

			if (Integer.valueOf(zero) == 0) {
				bclExcuteOk = true;
			}
		}

		return bclExcuteOk;
	}

	/**
	 * 
	 * @Title: base64StringToImage4One
	 * @Description: 图片处理
	 * @param @param base64String
	 * @param @param path 设定文件
	 * @return void 返回类型
	 * @throws
	 */
	public static void base64StringToImage4One(String base64String, String path) {
		try {		
			
			byte[] bytes = decoder.decodeBuffer(base64String);
			ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
			//APP用户图片尺寸设置100px * 100px 
			Image srcImg = ImageIO.read(bais);  
			BufferedImage buffImg = null;  
			buffImg = new BufferedImage(150, 150, BufferedImage.TYPE_INT_RGB);  
			buffImg.getGraphics()
			.drawImage(srcImg.getScaledInstance(150, 150, Image.SCALE_SMOOTH), 0, 0, null);  

			// 不管输出什么格式图片，此处不需改动
			// 可以是jpg,png,gif格式
			File file = new File(path + ".jpg");
			ImageIO.write(buffImg, "jpg", file);

		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 
	 * @Title: base64StringToImage4One
	 * @Description: 图片处理
	 * @param @param base64String
	 * @param @param path 设定文件
	 * @return void 返回类型
	 * @throws
	 */
	public static void base64StringToImage4One2(String base64String, String path) {
		try {		
			
			byte[] bytes = decoder.decodeBuffer(base64String);
			ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
			//APP用户图片尺寸设置100px * 100px 
//			Image srcImg = ImageIO.read(bais);  
//			BufferedImage buffImg = null;  
//			buffImg = new BufferedImage(width, height, imageType);  
//			buffImg.getGraphics()
//			.drawImage(srcImg, 0, 0, null);  

			// 不管输出什么格式图片，此处不需改动
			// 可以是jpg,png,gif格式
			File file = new File(path + ".jpg");
			//ImageIO.write(buffImg, "jpg", file);
			OutputStream out = new FileOutputStream(file);
			out.write(bytes);
			out.flush();
			out.close();

		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 
	 * @Title: sendMail
	 * @Description:邮件发送
	 * @param @param target 目标接口对象
	 * @param @param mail 邮件对象
	 * @param @return 设定文件
	 * @return boolean 返回类型
	 */
	public static boolean sendMail(IMailServlet target, MailVo mail) {
		Properties props = new Properties();
		props.setProperty("mail.SMTP.host", CommonUtils.getSys_mail_service());
		props.setProperty("mail.SMTP.auth", "true");
		// 标识
		boolean flag = false;

		try {

			Session session = Session.getInstance(props);
			session.setDebug(true);

			Message message = target.getMessageBySession(session, mail);

			Transport tran = session.getTransport("smtp");
			tran.connect(CommonUtils.getSys_mail_service(),
					CommonUtils.getSys_mail_account(),
					CommonUtils.getSys_mail_pwd());

			// 发送邮件并关闭传输对象
			tran.sendMessage(message, message.getAllRecipients());
			tran.close();

			flag = true;
		} catch (Exception e) {
			e.printStackTrace();
		}

		return flag;
	}

	/**
	 * 
	 * @Title: getVerCodeWith6Bit
	 * @Description: 随机生成6位验证码
	 * @param @return 设定文件
	 * @return String 返回类型
	 */
	public static String getVerCodeWith6Bit() {
		// 产生随机数
		Random random = new Random();
		String code = "";
		for (int i = 0; i < 6; i++) {

			int r = random.nextInt(9);
			code += String.valueOf(r);
		}
		return code;
	}

	/**
	 * 
	 * @Title: sendSMSInland
	 * @Description: 短信发送
	 * @param @param mobileno
	 * @param @param content
	 * @param @return
	 * @param @throws UnsupportedEncodingException 设定文件
	 * @return String 返回类型
	 */
	public static String sendSMSInland(String mobileno, String content)
			throws UnsupportedEncodingException {

		Sms_Service smsService = new Sms_Service();

		String result = "";
		try {
			result = smsService
					.getSmsHttpPort()
					.sms(CommonUtils.getEnterNo(),
							CommonUtils.getUsername(),
							CommonUtils.getUserpassword(),
							content,
							mobileno,
							DateUtil.getCurrentTime(DateUtil.ALL_DATETIME_STRING_QUEUE_MILL),
							"", "", "", "", "");
		} catch (ParseException e) {
			e.printStackTrace();
		}

		// result=0&description=发送短信成功&taskid=10131276929&faillist=
		System.out.println("result:"+result);
		String[] resultArry = result.split("&");
		String zero = resultArry[0].replaceAll("result=", "");
		return zero;

	}

	/**
	 * 
	 * @Title: timeTotal
	 * @Description: 时间段相加
	 * @param @param 字符串数组,timeList:{ "00:36:30", "00:23:43" }
	 * @param @return 设定文件
	 * @return String 返回类型
	 * @throws
	 */
	public static String timeTotal(String[] timeList) {

		String timeString = null;
		int hour = 0;
		int min = 0;
		int sec = 0;
		for (int i = 0; i < timeList.length; i++) {
			timeString = timeList[i];
			int firstIndex = timeString.indexOf(':');
			int secondIndex = timeString.lastIndexOf(':');
			hour += Integer.parseInt(timeString.substring(0, firstIndex));
			min += Integer.parseInt(timeString.substring(firstIndex + 1,
					secondIndex));
			sec += Integer.parseInt(timeString.substring(secondIndex + 1,
					timeString.length()));
		}
		int secToMin, sec2 = sec;
		int minToHour, min2 = min;
		if (sec >= 60) {
			secToMin = (sec / 60);
			sec2 = (sec % 60);
			min = min + secToMin;
		}

		if (min >= 60) {
			minToHour = (min / 60);
			min2 = (min % 60);
			hour = hour + minToHour;
		}
		String hString;
		String mString;
		String string;
		if (hour < 10) {
			hString = "0" + String.valueOf(hour);
		} else {
			hString = String.valueOf(hour);
		}
		if (min2 < 10) {
			mString = "0" + String.valueOf(min2);
		} else {
			mString = String.valueOf(min2);
		}
		if (sec2 < 10) {
			string = "0" + String.valueOf(sec2);
		} else {
			string = String.valueOf(sec2);
		}
		return hString + ":" + mString + ":" + string;
	}
	/*
	 * public static void main(String[] args) throws
	 * UnsupportedEncodingException { String str =sendSMSInland("18221480269",
	 * "恭喜您报名成功，欢迎加入哈哈哈哈哈哈哈 ，时间：xxx 地点：孟向璐璐璐 88 号，请提前阅读活动/课程须知。从现在开始，有点自己的功夫");
	 * System.out.println(str); }
	 */
	
	 
}
